package com.timmy.smarttest.presenter;

import android.text.TextUtils;

import com.inuker.bluetooth.library.search.SearchResult;
import com.timmy.smarttest.callback.BleCallbackImpl;
import com.timmy.smarttest.ui.MainActivity;
import com.timmy.smarttest.utis.BleUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Timmy
 * @Data: 2017/7/4
 * @Fuction:
 */

public class MainActivityPresenter {
    private MainActivity mActivity;
    public BleUtil mBleUtil;

    // 扫描的蓝牙集合
    private List<SearchResult> mResults;
    // 连接过的蓝牙集合
    public List<SearchResult> mConnectList;

    public MainActivityPresenter(MainActivity activity) {
        mBleUtil = BleUtil.getInstance();
        mActivity = activity;

        initData();
    }

    private void initData() {
        mResults = new ArrayList<>();
        mConnectList = new ArrayList<>();
    }

    public void registerNotifyListener() {
        mBleUtil.registerNotifyListener(mBleCallback);
    }

    public void scanBleDevice() {
        // 扫描蓝牙设备
        mBleUtil.search();
    }

    private SearchResult mConnectDevice;

    // 连接设备
    public void connectDevice(SearchResult device) {
        mConnectDevice = device;
        mBleUtil.connect(device.getAddress());
    }

    public boolean getBleState() {
        return mBleUtil.getBleState();
    }

    // 断开蓝牙连接
    public void disConnect() {
        mBleUtil.disconnect();
    }

    // 发送数据
    public void sendData(int id) {
        switch (id) {
            case 1:
                mBleUtil.sendData((byte) 0x51, (byte) 0x01);
                break;
            case 2:
                mBleUtil.sendData((byte) 0x52, (byte) 0x01);
                break;
            case 3:
                break;
            case 4:
                mBleUtil.sendData((byte) 0x41);
                break;
            case 5:
                mBleUtil.sendData((byte) 0x42);
                break;
            case 6:
                mBleUtil.sendData((byte) 0x54, (byte) 0x01);
                break;
            case 7:
                mBleUtil.sendData((byte) 0x90);
                break;
        }
    }

    // 发送停止数据
    public void sendPauseData(int id) {
        switch (id) {
            case 1:
                mBleUtil.sendData((byte) 0x51, (byte) 0x00);
                break;
            case 2:
                mBleUtil.sendData((byte) 0x52, (byte) 0x00);
                break;
            case 3:
                break;
            case 4:
                mBleUtil.sendData((byte) 0x41, (byte) 0x02);
                break;
            case 5:
                // mBleUtil.sendData((byte) 0x42);
                // 紫外线未知
                break;
            case 6:
                mBleUtil.sendData((byte) 0x54, (byte) 0x00);
                break;
            case 7:
                mBleUtil.sendData((byte) 0x90, (byte) 0x02);
                break;
        }
    }

    BleCallbackImpl mBleCallback = new BleCallbackImpl() {

        @Override
        public void onScan(SearchResult device) {
            // 扫描到的设备
            String name = device.getName();
            if (!TextUtils.isEmpty(name) && name.contains("S002")) {
                if (!mConnectList.contains(device) && !mResults.contains(device)) {
                    mResults.add(device);
                }
            }
        }

        @Override
        public void onScanStop() {
            //扫描完成
            mActivity.getDevice();
            mActivity.upData(mResults, mConnectList);
        }

        @Override
        public void onBleConnected() {
            mConnectList.add(mConnectDevice);
            mResults.remove(mConnectDevice);
            // 蓝牙连接成功
            mActivity.onConnectSucc();
            mActivity.upData(mResults, mConnectList);
        }

        @Override
        public void onBleDisconnected() {
            // 蓝牙连接失败
            mActivity.onConnectFaild();
        }
    };
}
